#include "pool3d.h"
void pool3d(hls::stream<BUS_TYPE> &in_stream,hls::stream<BUS_TYPE> &out_stream,
int ch_div_K,int height_in,int width_in,int height_out,int width_out,int kx,int sx)
{
  	hls::stream<BUS_TYPE> temp_stream;
  	pool_s1(in_stream, temp_stream, ch_div_K, height_in, width_in, kx, sx);
}
void pool_s1(hls::stream<BUS_TYPE> &in_stream,hls::stream<BUS_TYPE> &temp_stream,
		int ch_div_K,int height_in,int width_in,int kx,int sx)
{
	CHANNEL_LOOP:for(int i=0;i<ch_div_K;i++)
	{
	HEIGHT_LOOP:for(int j=0;j<height_in;j++)
		{
			WIDTH_LOOP:for(int m=0;m<width_in;m++)
			{
				BUS_TYPE temp=in_stream.read();
				BUS_TYPE out;
				int over_kx=m-kx;
				if(m==0||(over_kx%sx==0&over_kx>=0))
				{
				     out=temp;
				}
				else
				{
			 K_LOOP:for(int n=0;n<K;n++)
					{
					#pragma HLS unroll
						  	int low_address=n<<4;
							out.data.range(low_address+15,low_address)=MAX(out.data.range(low_address+15,low_address),temp.data.range(low_address+15,low_address));
					}
				}

				if((over_kx+1)>=0&(over_kx+1)%sx==0)
					temp_stream.write(out);
			}
		}
	}
}
